package com.android.personal.edit;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

import com.android.personal.data.FinanceDataAdaptater;
import com.android.personal.data.FinanceDataTags;
import com.android.personal.finance.R;

public class OperationsEditActivity extends Activity {
	
    private int mYear;
    private int mMonth;
    private int mDay;

    static final int DATE_DIALOG_ID = 0;
    
	private EditText dateEdit;
    
    private Cursor accountsCursor;
    
    private int accountId;
    
    
	public DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
            	
            	mYear = year;
            	mMonth = monthOfYear;
            	mDay = dayOfMonth;
            	updateDate();
            }
        };
    
	public void onCreate(Bundle savedInstanceState) {
	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.operation_edit);

		dateEdit = (EditText) findViewById(R.id.operation_date);
		Button confirmButton = (Button) findViewById(R.id.confirm);
	
		final Calendar calendar = Calendar.getInstance();
		mYear = calendar.get(Calendar.YEAR);
		mMonth = calendar.get(Calendar.MONTH);
		mDay = calendar.get(Calendar.DAY_OF_MONTH);

        updateDate();
		
		dateEdit.setOnTouchListener(new View.OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				
				showDialog(DATE_DIALOG_ID);
				return false;
			}
		});
		
		confirmButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	
            	Bundle bundle = new Bundle();
                
                bundle.putInt(FinanceDataTags.OPERATION_YEAR,mYear);
                bundle.putInt(FinanceDataTags.OPERATION_MONTH,mMonth);
                bundle.putInt(FinanceDataTags.OPERATION_DAY,mDay);
                
                bundle.putInt(FinanceDataTags.OPERATION_ACCOUNT_ID, accountId);
                
                Intent mIntent = new Intent();
                mIntent.putExtras(bundle);
                setResult(RESULT_OK, mIntent);
            	finish();
            }
        });
		
		Spinner spin = (Spinner) findViewById(R.id.operation_account);
		accountsCursor = FinanceDataAdaptater.fetchAccounts();
		startManagingCursor(accountsCursor);
		
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
			    android.R.layout.simple_spinner_item,
			    accountsCursor,
			    new String[] {FinanceDataTags.ACCOUNT_NAME}, 
			    new int[] {android.R.id.text1});
			         
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(adapter);
		spin.setOnItemSelectedListener(new AccountsSpinnerListener());
	}
	
	protected Dialog onCreateDialog(int id) {
	    
		switch (id) {
	    case DATE_DIALOG_ID:
	        return new DatePickerDialog(this,mDateSetListener,mYear, mMonth, mDay);
	    }
	    return null;
	}
	
	private void updateDate() {

		dateEdit.setText(
                new StringBuilder()
                .append(mMonth + 1).append("-")
                .append(mDay).append("-")
                .append(mYear).append(" "));
	}
	
		
	/*************************************************/
	
	/***
	 * Accounts Spinner Listener Overrides.
	 * 
	 */
	
	
	private class AccountsSpinnerListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent,
	        View view, int pos, long id) {
	      accountId = (int) id;
	    }

	    public void onNothingSelected(AdapterView<?> parent) {
	      ;
	    }
	}
	
	/*************************************************/
}
